package com.yougou.ui.login;

import android.os.Bundle;
import android.widget.ImageView;

import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputEditText;
import com.yougou.R;
import com.yougou.base.BaseActivity;
import com.yougou.utils.ToastUtils;
import com.yougou.utils.UserDataManager;

public class ForgotPasswordActivity extends BaseActivity {
    
    private ImageView mBtnBack;
    private TextInputEditText mEtUsername;
    private TextInputEditText mEtPhone;
    private TextInputEditText mEtNewPassword;
    private TextInputEditText mEtConfirmPassword;
    private MaterialButton mBtnReset;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgot_password);
        initView();
        initData();
    }
    
    @Override
    protected void initView() {
        mBtnBack = findViewById(R.id.iv_back);
        mEtUsername = findViewById(R.id.et_username);
        mEtPhone = findViewById(R.id.et_phone);
        mEtNewPassword = findViewById(R.id.et_new_password);
        mEtConfirmPassword = findViewById(R.id.et_confirm_password);
        mBtnReset = findViewById(R.id.btn_reset);
        
        mBtnBack.setOnClickListener(v -> finish());
        
        mBtnReset.setOnClickListener(v -> resetPassword());
    }
    
    @Override
    protected void initData() {
    }
    
    private void resetPassword() {
        String username = mEtUsername.getText().toString().trim();
        String phone = mEtPhone.getText().toString().trim();
        String newPassword = mEtNewPassword.getText().toString();
        String confirmPassword = mEtConfirmPassword.getText().toString();
        
        if (username.isEmpty()) {
            ToastUtils.show(this, "请输入用户名");
            return;
        }
        
        if (phone.isEmpty()) {
            ToastUtils.show(this, "请输入手机号");
            return;
        }
        
        if (!isValidPhone(phone)) {
            ToastUtils.show(this, "请输入正确的手机号");
            return;
        }
        
        if (newPassword.isEmpty()) {
            ToastUtils.show(this, "请输入新密码");
            return;
        }
        
        if (newPassword.length() < 6) {
            ToastUtils.show(this, "密码长度不能少于6位");
            return;
        }
        
        if (!newPassword.equals(confirmPassword)) {
            ToastUtils.show(this, "两次密码输入不一致");
            return;
        }
        
        if (!UserDataManager.verifyUserAndPhone(username, phone)) {
            ToastUtils.show(this, "用户名和手机号不匹配");
            return;
        }
        
        if (UserDataManager.resetPasswordByUsernameAndPhone(username, phone, newPassword)) {
            ToastUtils.show(this, "密码重置成功，请使用新密码登录");
            finish();
        } else {
            ToastUtils.show(this, "密码重置失败，请稍后再试");
        }
    }
    
    private boolean isValidPhone(String phone) {
        return phone.matches("^1[3-9]\\d{9}$");
    }
}

